package rgbos.forum.tests;

import java.text.SimpleDateFormat;
import java.util.Vector;

import rgbos.forum.domain.Auditor;
import rgbos.forum.domain.Post;
import rgbos.forum.persistance.Persistance;
import rgbos.forum.search.AbstractSearchEngine;
import rgbos.forum.search.CompassSearchEngine;
import rgbos.forum.search.PostRate;

import junit.framework.TestCase;

public class SearchEngineTest extends TestCase {

	private Vector<Post> _posts;
	private AbstractSearchEngine _search;

	protected void setUp() throws Exception {
		super.setUp();
//		Auditor.AuditorSet("search.log");
		_posts = new Vector<Post>();
		SimpleDateFormat tDateFormat = new SimpleDateFormat("dd/MM/yyyy");
		_posts.add(new Post(0,"Test0","Author0",tDateFormat.parse("01/06/2009"),"This is a test to see if I can search for posts"));
		_posts.add(new Post(1,"Test1","Author1",tDateFormat.parse("02/06/2009"),"I want to write something different here so I can test this"));
		_posts.add(new Post(2,"Test2","Author2",tDateFormat.parse("02/06/2009"),"This is how you do it in my world"));
		_posts.add(new Post(3,"Test3","Author3",tDateFormat.parse("03/06/2009"),"I'm looking is for stuff to write but I can find any this"));
		_posts.add(new Post(4,"Test4","Author1",tDateFormat.parse("04/06/2009"),"This is the last post! No more posts, this Search rules!"));

		_search = new CompassSearchEngine();

		for (Post message : _posts) {
			_search.indexPost(message);
		}		
	}
	
	protected void tearDown() {
		for (Post message : _posts) {
			_search.deletePost(message);
		}
	}
	
	private boolean contains(Vector<PostRate> tSearchResult, Post post) {
		boolean ans = false;
		for (PostRate searchResult : tSearchResult) {
			ans = ans | (searchResult.getPost().getId_() == post.getId_()); 
		}
		return ans;
	}
	
	public void testSearchSeveralPosts() {
		Vector<PostRate> tSearchResult = _search.searchByContent("search");
		assertTrue(contains(tSearchResult,_posts.get(0)));
		assertTrue(contains(tSearchResult,_posts.get(4)));
	}

	public void testSearchByDateOnePost() {
		Vector<PostRate> tSearchResult = _search.searchByDate("01/06/2009");		
		assertTrue(contains(tSearchResult,_posts.get(0)));
	}

	public void testSearchByDateSeveralPosts() {
		Vector<PostRate> tSearchResult = _search.searchByDate("02/06/2009");
		assertTrue(contains(tSearchResult,_posts.get(1)));
		assertTrue(contains(tSearchResult,_posts.get(2)));		
	}

	public void testSearchByAuthorOnePost() {
		Vector<PostRate> tSearchResult = _search.searchByAuthor("Author0");
		assertTrue(contains(tSearchResult,_posts.get(0)));		
	}

	public void testSearchByAuthorSeveralPosts() {
		Vector<PostRate> tSearchResult = _search.searchByAuthor("author1");
		assertTrue(contains(tSearchResult,_posts.get(1)));
		assertTrue(contains(tSearchResult,_posts.get(4)));		
	}

	public void testSearchPhrase() {
		Vector<PostRate> tSearchResult = _search.searchByContent("\"I want to\"");		
		assertTrue(contains(tSearchResult,_posts.get(1)));
		assertFalse(contains(tSearchResult,_posts.get(0)));
		assertFalse(contains(tSearchResult,_posts.get(2)));
		assertFalse(contains(tSearchResult,_posts.get(3)));
		assertFalse(contains(tSearchResult,_posts.get(4)));
	}

	public void testSearchSeveralWords() {
		Vector<PostRate> tSearchResult = _search.searchByContent("This posts");
		assertTrue(contains(tSearchResult,_posts.get(0)));
		assertTrue(contains(tSearchResult,_posts.get(4)));		
	}

	public void testSearchOnePost() {
		Vector<PostRate> tSearchResult = _search.searchByContent("see");
		assertTrue(contains(tSearchResult,_posts.get(0)));		
	}


	public void testNoCaseSensative() {
		Vector<PostRate> tSearchResult = _search.searchByContent("sEarCh");
		assertTrue(contains(tSearchResult,_posts.get(0)));
		assertTrue(contains(tSearchResult,_posts.get(4)));		
	}

	public void testSpecialChars() {
		Vector<PostRate> tSearchResult = _search.searchByContent("post");
		assertTrue(contains(tSearchResult,_posts.get(4)));		
	}
}
